//
//  ViewController.swift
//  SpoetStore
//
//  Created by 崔博 on 2017/4/16.
//  Copyright © 2017年 崔博. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var countLabel: UILabel!
    @IBOutlet weak var table: UITableView!
    var products = [
        ("Kayak", "A boat for one person", "Watersports", 275.0, 10),
        ("Lifejacket", "Protective and fashionable", "Watersports", 48.95, 14),
        ("Soccer Ball", "FIFA-approved size and weight", "Soccer", 19.5, 32),
        ("Corner Flags", "Give your playing field a professional touch",
         "Soccer", 34.95, 1),
        ("Stadium", "Flat-packed 35,000-seat stadium", "Soccer", 79500.0, 4),
        ("Thinking Cap", "Improve your brain efficiency by 75%", "Chess", 16.0, 8),
        ("Unsteady Chair", "Secretly give your opponent a disadvantage",
         "Chess", 29.95, 3),
        ("Human Chess Board", "A fun game for the family", "Chess", 75.0, 2),
        ("Bling-Bling King", "Gold-plated, diamond-studded King",
         "Chess", 1200.0, 4)];
    override func viewDidLoad() {
        super.viewDidLoad()
        
        table .register(UINib.init(nibName: "ProduceCell", bundle: nil), forCellReuseIdentifier: "ProduceCell")
        displayStockToatal()
        

    }

}

extension ViewController:UITableViewDelegate,UITableViewDataSource{
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return products.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = table.dequeueReusableCell(withIdentifier: "ProduceCell") as! ProduceCell
        let product = products[indexPath.row]
        cell.namelabel.text = product.0
        cell.deslabel.text = product.1
        cell.stepper.value = Double(product.4)
        cell.stockfield.text = String(product.4)

        
        return cell
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 100
    }
    func displayStockToatal() {
        let stackTotal = products.reduce(0, {(total, product) -> Int in return total + product.4})
        countLabel.text = "\(stackTotal) Products in stock"
    }
}
